package com.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.app.common.Result;
import com.app.model.ExposureModel;
import com.app.service.ExposureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 曝光台
 * -曝光台：
 * ->点击进入：新闻展示->点击查看详情
 * ->点击搜索框：关键字查询
 */
@Controller
@RestController
@RequestMapping("/api/exposure")
public class ExposureController {

    @Autowired
    private ExposureService exposureService;

    //查询全部
    @GetMapping("/all")
    public Result<Object> getAll(int page, int limit, String exposureTitle, Integer type) {
        ExposureModel model = new ExposureModel();
        model.setPage((page - 1) * limit);
        model.setLimit(limit);
        model.setExposureTitle(exposureTitle);
        model.setType(type);
        if (exposureTitle == null || exposureTitle.equals("")) {
            return Result.success(exposureService.ExposureList(model));
        }
        return Result.success(exposureService.List(model));
    }

    //  /exposure/content   get请求 参数 id
    //查看内容
    @GetMapping("/content")
    public Result<Object> getContentById(int id) {
        ExposureModel contentById = exposureService.getContentById(id);
        return Result.success(contentById);
    }

    /*
    曝光台后台管理
     */
    //添加通知公告
    @PostMapping("/add")
    public Result<Object> addExposure(@RequestBody JSONObject json) {
        String title = (String) json.get("exposureTitle");
        String content = (String) json.get("exposureContent");
        Integer isHeat = (Integer) json.get("isHeat");
        Integer sort = (Integer) json.get("sort");
        Integer type = (Integer) json.get("type");
        Integer id = (Integer) json.get("id");
        return Result.success(exposureService.add(title, content, sort, isHeat, type));
    }

    //是否热门  /exposure/heat
    @GetMapping("/heat")
    public Result<Object> heat(int id, int isHeat) {
        exposureService.selectById(id, isHeat);
        return Result.success("修改成功");
    }

    //编辑通知公告
    @PostMapping("/update")
    public Result<Object> updateExposure(@RequestBody JSONObject json) {
        String title = (String) json.get("exposureTitle");
        String content = (String) json.get("exposureContent");
        Integer isHeat = (Integer) json.get("isHeat");
        Integer sort = (Integer) json.get("sort");
        Integer id = (Integer) json.get("id");
        Integer type = (Integer) json.get("type");
        return Result.success(exposureService.update(title, content, sort, isHeat, id, type));
    }

    //删除通知公告
    @GetMapping("/delete")
    public Result<Object> deleteExposureById(int id) {
        exposureService.deleteExposureById(id);
        return Result.success("操作成功！");
    }

    //曝光台分类查询
    @GetMapping("/type")
    public Result<Object> selectType() {
        return Result.success(exposureService.selectType());
    }
}
